<template>
	<view class="cc-keyboard-wrap" v-if="visible">
		<view class="cc-keyboard-close" @tap="close"><uni-icons color="#999" type="down" size="16"></uni-icons></view>
		<view class="cc-keyboard">
			<view v-if="type === 'digit'" class="digit-mode">
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('1')">
					1
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('2')">
					2
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('3')">
					3
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('回退')">
					<uni-icons custom-prefix="iconfont" type="icon-backspace-fill" size="20" color="#222" />
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('4')">
					4
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('5')">
					5
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('6')">
					6
				</view>
				<view class="item confirm" hover-class="confirm-curr" hover-start-time="0" hover-stay-time="0" :style="{
		        backgroundColor:
		          value.charAt(value.length - 1) === '.' ? '#999' : '#000',
		      }" @click="onKeyboard('确定')">
					确定
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('7')">
					7
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('8')">
					8
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('9')">
					9
				</view>
				<view class="item zero" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('0')">
					0
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('.')">
					.
				</view>
			</view>
			<view v-if="type === 'password'" class="password-mode">
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('1')">
					1
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('2')">
					2
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('3')">
					3
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('4')">
					4
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('5')">
					5
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('6')">
					6
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('7')">
					7
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('8')">
					8
				</view>
				<view class="item" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('9')">
					9
				</view>
				<view class="item empty"> </view>
				<view class="item zero" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('0')">
					0
				</view>
				<view class="item empty" hover-class="current" hover-start-time="0" hover-stay-time="0"
					@click="onKeyboard('回退')">
					<uni-icons custom-prefix="iconfont" type="icon-backspace-fill" size="20" color="#222" />
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		getCurrentInstance
	} from 'vue';
	const props = defineProps({
		type: {
			type: String,
			default: 'digit',
			validator: (val) => ['digit', 'password'].includes(val)
		},
		focus: {
			type: Boolean,
			required: false,
		},
		value: {
			type: String,
			default: '',
		},
		background: {
			type: String,
			required: false,
			default: "#ededed",
		},
	});

	const emits = defineEmits([
		"update:value",
		"check",
		"confirm",
		"submit",
		"close",
	]);

	const popupRef = ref(null);
	const visible = ref(false);

	const show = () => {
		visible.value = true;
	}

	const close = () => {
		visible.value = false;
	}

	defineExpose({
		show,
	});
</script>

<style lang="scss" scoped>
	@import "@/cc-ui-tools/scss/theme.scss";
	@import "@/cc-ui-tools/scss/animation.scss";

	.cc-keyboard-wrap {
		position: fixed;
		width: 100%;
		bottom: 0px;
		animation: fadeIn 0.2s linear both;
		animation: fadeInUp 0.2s 0.2s linear both;
		padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
		border-radius: 10px 10px 0 0;
		backdrop-filter: blur(10rpx); //毛玻璃属性
		box-shadow: 0 -10px 20px rgba(0, 0, 0, 0.1); //阴影
		z-index: 99999;
		border-top: 1px solid #ececec;
		background-color: #f6f6f6;
	}

	.cc-keyboard-close {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 60rpx;
	}

	.cc-keyboard {
		padding: 0 18rpx 18rpx 18rpx;
		padding-bottom: calc(18rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(18rpx + env(safe-area-inset-bottom));

		.digit-mode {
			display: grid;
			grid-template-columns: repeat(4, 1fr);
			grid-template-rows: repeat(4, 1fr);
			gap: 18rpx;

			.item {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 18rpx 0;
				font-size: 36rpx;
				color: #222;
				font-weight: 600;
				border-radius: 10rpx;
				background-color: #fff;

				&.zero {
					grid-column: span 2;
				}

				&.confirm {
					grid-row: span 3;
					font-size: 28rpx;
					color: #fff;
					//background-color: #57be6b;
					background-color: #000;
				}

				&.current {
					background-color: #d7d7d7;
				}

				&.confirm-curr {
					//background-color: #6bee88;
					background-color: #000;
				}
			}
		}

		.password-mode {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			grid-template-rows: repeat(4, 1fr);
			gap: 18rpx;

			.item {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 18rpx 0;
				font-size: 36rpx;
				color: #222;
				font-weight: 600;
				border-radius: 10rpx;
				background-color: #fff;

				&.empty {
					background-color: rgba(0, 0, 0, 0);
				}

				&.current {
					background-color: #d7d7d7;
				}
			}
		}
	}
</style>